﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CopyPathFile
{
    public partial class Form1 : Form
    {
        CopyPath CopyManager = new CopyPath();
        private delegate void ChangeEndStatusHandler();
        delegate void ChangeStartStatusHandler(CopyFile file);
        delegate void ChangeCompleteStatusHandler();
        Configuration config = new Configuration();
        public Form1()
        {
            InitializeComponent();
            CopyManager.OnSearch += new SearchHandler(CopyManager_OnSearch);
            CopyManager.OnSearchEnd += new EventHandler(CopyManager_OnSearchEnd);
            CopyManager.OnCopy += new CopyHandler(CopyManager_OnCopy);
            CopyManager.OnComplete += new CompleteHandler(CopyManager_OnComplete);
            CopyManager.OnError += new ErrorHandler(CopyManager_OnError);

            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);

            txtFilter.Text = config.Filter;
            lblLastestTime.Text = config.LatestTime;
            txtSource.Text = config.Source;
            txtTarget.Text = config.Target;

        }

        private void SaveConfig()
        {
            config.Filter = txtFilter.Text;
            config.LatestTime = DateTime.Now.ToString();
            config.Source = txtSource.Text;
            config.Target = txtTarget.Text;
        }
        private void Form1_FormClosing(object sender, EventArgs e)
        {
            SaveConfig();
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Enabled = false;
            btnSearch.Cursor = System.Windows.Forms.Cursors.AppStarting;
            CopyManager.Filter = this.txtFilter.Text.Split(';');
            CopyManager.SourcePath = this.txtSource.Text;
            CopyManager.TargetPath = this.txtTarget.Text;
            lstPath.Items.Clear();
            lblCopiedCount.Text = "0";
            lblTotalCount.Text = "0";
            System.Threading.Thread thread = new System.Threading.Thread(CopyManager.Search);
            thread.IsBackground = true;
            thread.Start();
        }

        private void CopyManager_OnError(string msg,CopyFile file)
        {
            if (lstError.InvokeRequired)
            {
                //lstPath.BeginInvoke(ChangeStatusHandler(ChangeStatus(file)),null);
                ErrorHandler csh = new ErrorHandler(ErrorStatus);
                this.BeginInvoke(csh,msg, file);
            }
            else
            {
                ErrorStatus( msg, file);
            }

        }

        private void CopyManager_OnSearchEnd(object sender, EventArgs e)
        {
            if (lstPath.InvokeRequired)
            {
                //lstPath.BeginInvoke(ChangeStatusHandler(ChangeStatus(file)),null);
                ChangeEndStatusHandler csh = new ChangeEndStatusHandler(ChangeEndStatus);
                this.BeginInvoke(csh);
            }
            else
            {
                ChangeEndStatus(); ;
            }

        }

        private void CopyManager_OnSearch(CopyFile file)
        {
            if (lstPath.InvokeRequired)
            {
                //lstPath.BeginInvoke(ChangeStatusHandler(ChangeStatus(file)),null);
                ChangeStartStatusHandler csh = new ChangeStartStatusHandler(ChangeStartStatus);
                this.BeginInvoke(csh, file);
            }
            else
            {
                ChangeStartStatus(file);
            }
        }

        private void CopyManager_OnCopy(CopyFile file)
        {
            if (lblCopiedCount.InvokeRequired)
            {
                ChangeStartStatusHandler csh = new ChangeStartStatusHandler(ChangeCopyStatus);
                this.BeginInvoke(csh, file);
            }
            else
            {
                ChangeCopyStatus(file);

            }

        }

        private void CopyManager_OnComplete()
        {
            if (btnCopy.InvokeRequired)
            {
                ChangeCompleteStatusHandler csh = new ChangeCompleteStatusHandler(ChangeCompleteStatus);
                this.BeginInvoke(csh);
            }
            else
            {
                ChangeCompleteStatus();

            }

        }

        private void ChangeCompleteStatus()
        {

            btnCopy.Enabled = true;
            btnCopy.Cursor = System.Windows.Forms.Cursors.Default;
            btnSearch.Enabled = true;
            btnSearch.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void ChangeCopyStatus(CopyFile file)
        {

            lblMsg.Text = file.Source;
            lblCopiedCount.Text = (int.Parse(lblCopiedCount.Text) + 1).ToString();
        }

        private void ChangeStartStatus(CopyFile file)
        {

            lstPath.Items.Add(file.Source);
            lblMsg.Text = file.Source;
            lblTotalCount.Text = CopyManager.AllFiles.Count.ToString();
        }

        private void ChangeEndStatus()
        {

            btnSearch.Enabled = true;
            btnSearch.Cursor = System.Windows.Forms.Cursors.Default;
            btnCopy.Enabled = true;
        }

        private void ErrorStatus(string msg, CopyFile file)
        {

            lstError.Items.Add(file.Source);
            lstError.Items.Add(msg);

        }

        private void lstPath_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(lstPath.SelectedItem.ToString());
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                MessageBox.Show("找不到文件", "提示");
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            btnCopy.Enabled = false;
            btnCopy.Cursor = System.Windows.Forms.Cursors.AppStarting;
            btnSearch.Enabled = false;
            btnSearch.Cursor = System.Windows.Forms.Cursors.AppStarting;
            lblCopiedCount.Text = "0";
            lstError.Items.Clear();
            Thread t = new Thread(new ThreadStart(CopyManager.DoCopy));
            t.IsBackground = true;
            t.Start();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtSource.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtTarget.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void lstPath_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                info.FileName = "explorer";
                info.Arguments = "/select, " + lstPath.SelectedItem.ToString();
                System.Diagnostics.Process.Start(info);
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                MessageBox.Show("找不到文件", "提示");
            }
        }




    }
}
